//
//  StatusDetailReweetOptionBar.m
//  SinaWeibo
//
//  Created by mj on 13-8-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "StatusDetailReweetOptionBar.h"
#import "AyStatus.h"
#import "NSStringExtend.h"
#import "UIImageExtend.h"
#import "AyDefine.h"
#define kBtnTextColor AyColor(147, 147, 147)

@interface StatusDetailReweetOptionBar()
{
    UIButton *_btnRepost; // 转发
    UIButton *_btnComment; // 评论
    UIButton *_btnAttitude; // 赞
}
@end

@implementation StatusDetailReweetOptionBar

- (void)setBtnText:(UIButton *)btn count:(int)count place:(NSString *)place
{
    if (count > 0) {
        place = [NSString withinWanStr:count];
    }
    
    [btn setTitle:place forState:UIControlStateNormal];
}

- (void)setStatus:(AyStatus *)status
{
    _status = status;
    
    [self setBtnText:_btnRepost count:status.reposts_count place:@"转发"];
    [self setBtnText:_btnComment count:status.comments_count place:@"评论"];
    [self setBtnText:_btnAttitude count:status.attitudes_count place:@"赞"];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (self.subviews.count || frame.size.width == 0) return;
    
    // 1.转发
    _btnRepost = [self addOptionBtnWithTitle:@"转发" icon:@"timeline_icon_retweet.png" index:0];
    
    // 2.评论
    _btnComment = [self addOptionBtnWithTitle:@"评论" icon:@"timeline_icon_comment.png" index:1];
    
    // 3.赞
    _btnAttitude = [self addOptionBtnWithTitle:@"赞" icon:@"timeline_icon_unlike.png" index:2];
}

#pragma mark 添加一个操作按钮
- (UIButton *)addOptionBtnWithTitle:(NSString *)title icon:(NSString *)icon index:(int)index
{
    CGFloat btnWidth = self.frame.size.width / 3;
    CGFloat btnHeight = self.frame.size.height;
    // 创建
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(index * btnWidth, 0, btnWidth, btnHeight);
    // 图片
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    btn.adjustsImageWhenHighlighted = NO;
    [btn setBackgroundImage:[UIImage resizedImageWithName:@"statusdetail_icon_highlighted.png"] forState:UIControlStateHighlighted];
    // 文字属性
    [btn setTitleColor:kBtnTextColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn setTitle:title forState:UIControlStateNormal];
    // 文字、图标间距
    btn.titleEdgeInsets = UIEdgeInsetsMake(1, 6, 0, 0);
    // 添加
    [self addSubview:btn];
    return btn;
}

@end